#ifndef DXRENDER_GUARD
#define DXRENDER_GUARD

#include <iostream>
#include "Render.h"
#include <d3d9.h>
#include <d3dx9.h>
#include "Log.h"

class DXRender: public Render
{
public:
	DXRender(HWND hwnd):d3d(NULL),d3dDevice(NULL),hWnd(hwnd){}
	void init()
	{
		assureSuccess
			(
			d3d = Direct3DCreate9(D3D_SDK_VERSION)
			)

			D3DDISPLAYMODE displayMode;

		assureSuccess
			(
			SUCCEEDED(d3d->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &displayMode))
			)

			D3DPRESENT_PARAMETERS d3dpp;
		ZeroMemory(&d3dpp, sizeof(d3dpp));

		d3dpp.Windowed = TRUE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.BackBufferFormat = displayMode.Format;

		assureSuccess
			(
			SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3dDevice))
			)
	}
	void begin()
	{
		//d3dDevice->BeginScene();
		std::cout<<"DXRender::begin()"<<std::endl;
	}
	void end()
	{
		//d3dDevice->EndScene();
		std::cout<<"DXRender::end()"<<std::endl;
	}
	void setClearColor(float r, float g, float b, float a)
	{
		clearColor_r=255*r;
		clearColor_g=255*g;
		clearColor_b=255*b;
		clearColor_a=255*a;
	}
	void clear()
	{
		//D3DColor c=Color_to_D3DColor(clearColor);
		d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(clearColor_r,clearColor_g,clearColor_b),1.0f,0);

		std::cout<<"DXRender::clear()"<<std::endl;
	}
	void setColor(float r,float g,float b,float a)
	{

	}
	void draw()
	{
		d3dDevice->BeginScene();

		d3dDevice->EndScene();

	}
	void swap()
	{
		d3dDevice->Present(NULL,NULL,NULL,NULL);
		std::cout<<"DXRender::swap()"<<std::endl;
	}

	~DXRender()
	{
		if (d3dDevice != NULL) d3d->Release();
		if (d3d != NULL) d3d->Release();

		d3dDevice = NULL;
		d3d = NULL;
	}

	int renderIndexedVertexArray(float* vertices,int vertices_count,int* indices,int indices_count)
	{
	
		IDirect3DVertexBuffer9* VB = 0;
		
		IDirect3DIndexBuffer9*  IB = 0;


		d3dDevice->CreateVertexBuffer(
			3*sizeof(float) * vertices_count, 
			D3DUSAGE_WRITEONLY,
			D3DFVF_XYZ,
			D3DPOOL_MANAGED,
			&VB,
			0);

		d3dDevice->CreateIndexBuffer(
			sizeof(int) * indices_count,
			D3DUSAGE_WRITEONLY,
			D3DFMT_INDEX32,
			D3DPOOL_MANAGED,
			&IB,
			0);

	
		void* vertices_ptr;
		VB->Lock(0, 0, (void**)&vertices_ptr, 0);
		memcpy(vertices_ptr,vertices,3*3*sizeof(float));
		VB->Unlock();


		void* indices_ptr = 0;
		IB->Lock(0, 0, (void**)&indices_ptr, 0);
		memcpy(indices_ptr,indices,3*sizeof(int));
		IB->Unlock();

		//d3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
		d3dDevice->BeginScene();

		d3dDevice->SetStreamSource(0, VB, 0, 3*sizeof(float));
		d3dDevice->SetIndices(IB);
		d3dDevice->SetFVF(D3DFVF_XYZ);
		d3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 3, 0, 1);

		d3dDevice->EndScene();

		return 0;
	}
private:
	LPDIRECT3D9 d3d;
	LPDIRECT3DDEVICE9 d3dDevice;
	HWND hWnd;
	int clearColor_r;
	int clearColor_g;
	int clearColor_b;
	int clearColor_a;
};


#endif